<template>
  <div class="store-basics-edit-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{title}}</span>
      </div>
      <el-form ref="form" id="form" :model="form" :rules="rules" label-width="135px">
        <el-row style="display: flex;justify-content: center;align-items: center;">
          <el-col :span="8">
            <el-form-item label="评价项名称" prop="projectName">
              <el-input v-model="form.projectName" placeholder="请输入评价项名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="评价项类型">
              <el-select @change="selectTypeChanged" v-model="form.projectType" placeholder="请选择评价项类型">
                <el-option v-for="item in typeList" :key="item.code" :label="item.type" :value="item.type" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="margin: 25px 0;display: flex;justify-content: center;align-items: center;">
            <el-button plain type="primary" size="small" @click="addChoiceItem()">添加选项</el-button>
        </el-row>

        <div v-if="item.isActive == 1" class="ChoiceItemsBox" v-for="(item, index) in form.xdSurveyChoices" :key="index">
          <el-row v-if="form.xdSurveyChoices.length > 1" style="margin: 25px 0 10px 0;">
            <el-col :span="6" style="display: flex;justify-content: flex-end;align-items: center;">
              <el-button class="delete-choice-item-btn" plain type="danger" size="small" @click="delChoiceItem(item.index)">删除该选项</el-button>
            </el-col>
          </el-row>
          <el-row style="display: flex;justify-content: center;align-items: center;">
            <el-col :span="8">
              <el-form-item label="选项名称" prop="choiceName">
                <el-input v-model="item.choiceName" placeholder="请输入选项名称" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="业绩系数" prop="achieveModulus">
                <el-input v-model="item.achieveModulus" placeholder="请输入业绩系数" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row style="display: flex;justify-content: center;align-items: center;">
            <el-col :span="8">
              <el-form-item label="评价自身得分" prop="evaluateOwnScore">
                <el-input v-model="item.evaluateOwnScore" placeholder="请输入评价自身得分" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="评价市场得分" prop="evaluateMarketScore">
                <el-input v-model="item.evaluateMarketScore" placeholder="请输入评价市场得分" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row style="display: flex;justify-content: center;align-items: center;">
            <el-col :span="8">
              <el-form-item label="新引入自身得分" prop="introduceOwnScore">
                <el-input v-model="item.introduceOwnScore" placeholder="请输入新引入自身得分" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="新引入市场得分" prop="newMarketScore">
                <el-input v-model="item.newMarketScore" placeholder="请输入新引入市场得分" />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <el-row style="display: flex;justify-content: center;align-items: center;margin-top: 25px;">
          <el-col :span="16">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入评价项名称备注" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-form-item class="form-btn-bar">
            <el-button type="primary" @click="submitForm">保 存</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  import {
    listProject,
    getProject,
    delProject,
    addProject,
    updateProject,
    exportProject
  } from "@/api/xdstore/project";
  import Editor from '@/components/Editor';

  import {
    TMap
  } from '../../../components/Map/TMap.js'

  export default {
    name: "ProjectEdit",
    components: {
      Editor
    },
    data() {
      return {
        title: '',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 门店评价项目表格数据
        projectList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          projectName: undefined,
          projectType: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          projectName: [{
            required: false,
            message: '请填写评价项名称',
            trigger: 'blur'
          }],
          projectType: [{
            required: false,
            message: '请选择评价项类型',
            trigger: 'blur'
          }]
        },
        typeList: [{
            type: '基础信息',
            code: 'BASE_INFO'
          },
          {
            type: '加盟相关信息',
            code: 'JM_INFO'
          },
          {
            type: '市场相关信息',
            code: 'MARKET_INFO'
          },
          {
            type: '门店相关信息',
            code: 'STORE_INFO'
          }
        ],
        usedChoiceItems: 1, // 已使用过的选择项索引ID（最大值，唯一）
      }
    },
    methods: {
      delChoiceItem(itemIndex) {
        const arr = [];
        const that = this;
        that.form.xdSurveyChoices.forEach(function(item) {
          if (item.index !== itemIndex) {
            arr.push(item);
          } else {
            if (that.title === "修改门店评价项目") {
              item.isActive = '0';
              arr.push(item);
            }
          }
        })
        that.form.xdSurveyChoices = arr;
        console.log('删除选项之后xdSurveyChoices:');
        console.log(that.form.xdSurveyChoices);
      },
      addChoiceItem() {
        const that = this;
        that.usedChoiceItems = that.usedChoiceItems + 1;
        const obj = {
          index: that.usedChoiceItems,
          choiceName: undefined,
          achieveModulus: undefined,
          evaluateOwnScore: undefined,
          evaluateMarketScore: undefined,
          introduceOwnScore: undefined,
          newMarketScore: undefined,
          lesseeId: '13',
          isActive: '1'
        };
        that.form.xdSurveyChoices.push(obj);
        console.log('新增选项之后xdSurveyChoices:');
        console.log(that.form.xdSurveyChoices);
      },
      selectTypeChanged(type) {
        const that = this;
        // 根据选择来填写form.projectTypeCode的值
        console.log(type);
        that.form.projectType = type;
        switch (type) {
          case '基础信息':
            that.form.projectTypeCode = 'BASE_INFO';
            break;
          case '加盟相关信息':
            that.form.projectTypeCode = 'JM_INFO';
            break;
          case '市场相关信息':
            that.form.projectTypeCode = 'MARKET_INFO';
            break;
          case '门店相关信息':
            that.form.projectTypeCode = 'STORE_INFO';
            break;
          default:
            break;
        }
      },
      /** 提交按钮 */
      submitForm: function() {
        const that = this;
        that.$refs["form"].validate(valid => {
          if (valid) {
            const params = {
              id: that.form.id,
              isActive: that.form.isActive,
              lesseeId: that.form.lesseeId,
              projectName: that.form.projectName,
              projectType: that.form.projectType,
              projectTypeCode: that.form.projectTypeCode,
              xdSurveyChoices: that.form.xdSurveyChoices,
              remark: that.form.remark
            };
            if (params.id != undefined) {
              updateProject(params).then(response => {
                if (response.code === 200) {
                  that.msgSuccess("修改成功");
                  that.open = false;
                  that.closeSelectedTag(that.$route);
                }
              });
            } else {
              addProject(params).then(response => {
                if (response.code === 200) {
                  that.msgSuccess("新增成功");
                  that.open = false;
                  that.closeSelectedTag(that.$route);
                }
              });
            }
          }
        });
      },
      isActive(route) {
        return route.path === this.$route.path
      },
      closeSelectedTag(view) {
        this.$store.dispatch('tagsView/delView', view).then(({
          visitedViews
        }) => {
          if (this.isActive(view)) {
            this.toLastView(visitedViews, view)
          }
        })
      },
      toLastView(visitedViews, view) {
        const latestView = visitedViews.slice(-1)[0]
        if (latestView) {
          this.$router.push(latestView.fullPath)
        } else {
          if (view.name === 'Dashboard') {
            this.$router.replace({
              path: '/redirect' + view.fullPath
            })
          } else {
            this.$router.push('/')
          }
        }
      },
      getProjectInfo(id) {
        const that = this;
        if (id != "new") {
          getProject(id).then(response => {
            console.log('详情:');
            console.log(response);
            that.form = response.data
            if (that.form.xdSurveyChoices && that.form.xdSurveyChoices.length>0) {
              let i = 1;
              that.form.xdSurveyChoices.forEach(function(item) {
                item.index = i;
                i = i + 1;
              })
              that.usedChoiceItems = that.form.xdSurveyChoices.length;
            }
            that.title = "修改门店评价项目"
          })
        } else {
          that.title = "新增门店评价项目"
        }
      },
      // 表单重置
      reset(call) {
        this.form = {
          id: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          isActive: 1,
          lesseeId: 13,
          projectType: undefined,
          projectTypeCode: undefined,
          projectName: undefined,
          remark: undefined,
          xdSurveyChoices: [{
            index: 1,
            choiceName: undefined,
            achieveModulus: undefined,
            evaluateOwnScore: undefined,
            evaluateMarketScore: undefined,
            introduceOwnScore: undefined,
            newMarketScore: undefined,
            lesseeId: '13',
            isActive: '1'
          }]
        };
        this.resetForm("form");
        if (call) {
          call()
        }
      }
    },
    created() {
      const that = this;
      that.reset(function() {
        console.log('获取到this.$route.params.id:' + that.$route.params.id);
        that.getProjectInfo(that.$route.params.id);
      })
    }
  }
</script>

<style>
  .editor-content .quill-editor.editor {
    height: 500px
  }

  .form-btn-bar {
    margin-top: 30px;
    text-align: center;
  }

  /*搜索框*/
  .search {
    width: 100%;
  }

  .input-group {
    position: relative;
    display: table;
    border-collapse: separate;
  }

  * {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
  }

  .btn {
    display: inline-block;
    padding: 6px 12px;
    margin-bottom: 0;
    font-size: 14px;
    font-weight: 400;
    line-height: 1.42857143;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    background-image: none;
    border: 1px solid transparent;
    border-radius: 4px;
  }

  .form-control {
    display: block;
    width: 100%;
    height: 34px;
    padding: 6px 12px;
    font-size: 14px;
    line-height: 1.42857143;
    color: #555;
    background-color: #fff;
    background-image: none;
    border: 1px solid #ccc;
    border-radius: 4px;
    -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
    box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
    -webkit-transition: border-color ease-in-out .15s, -webkit-box-shadow ease-in-out .15s;
    -o-transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
    transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
  }

  .input-group .form-control:first-child,
  .input-group-addon:first-child,
  .input-group-btn:first-child>.btn,
  .input-group-btn:first-child>.btn-group>.btn,
  .input-group-btn:first-child>.dropdown-toggle,
  .input-group-btn:last-child>.btn-group:not(:last-child)>.btn,
  .input-group-btn:last-child>.btn:not(:last-child):not(.dropdown-toggle) {
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
  }

  .input-group .form-control,
  .input-group-addon,
  .input-group-btn {
    display: table-cell;
  }

  .input-group .form-control {
    position: relative;
    z-index: 2;
    float: left;
    width: 100%;
    margin-bottom: 0;
  }

  button,
  input,
  select,
  textarea {
    font-family: inherit;
    font-size: inherit;
    line-height: inherit;
  }

  button,
  input,
  optgroup,
  select,
  textarea {
    margin: 0;
    font: inherit;
    color: inherit;
  }

  .input-group .form-control.getAddress {
    width: 85%;
  }

  .searchBtn {
    width: 15%;
    float: right;
    border-radius: 0 4px 4px 0;
    background: #dca131;
    color: #fff;
  }

  /*提示语*/
  .prompt {
    color: #e84720;
    font-size: 14px;
    font-family: '微软雅黑';
    margin: 5px 10px;
  }

  .validate {
    position: absolute;
    bottom: 10px;
    right: 20px;
    color: #e84720;
  }

  .test {
    display: inline-block;
    width: 20px;
    height: 10px;
    background: rgba(239, 194, 192, 0.38);
  }

  /*地图*/
  .map {
    padding: 10px 30px;
    border: 1px solid #ddd;
    border-radius: 10px;
    margin-bottom: 20px;
  }

  #containerMap {
    margin: 10px auto;
    width: 100%;
    min-height: 470px;
  }

  /*经纬度设置*/
  .latLng {
    text-align: center;
    margin: 20px auto;
  }

  .latlngSpan {
    font-size: 16px;
  }

  .latlngInput {
    width: 200px;
    height: 25px;
    outline: none;
    background: #fff;
  }
</style>
